package com.zhang.webadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.webadmin.pojo.AclRole;
import com.zhang.webadmin.pojo.AclRolePermission;
import com.zhang.webadmin.service.AclRolePermissionService;
import com.zhang.webadmin.service.AclRoleService;
import com.zhang.webadmin.util.R;
import com.zhang.webadmin.vo.SetRolePermissionsVo;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

// 角色管理
@RestController
@RequestMapping("/aclrole")
@Validated
public class AclRoleController {
    @Resource
    private AclRoleService aclRoleService;
    @Resource
    private AclRolePermissionService aclRolePermissionService;


    @PreAuthorize("hasAuthority('aclrole:query')")
    @GetMapping("/list")
    public R list(@RequestParam(required = false, defaultValue = "1") Integer page,
                  @RequestParam(required = false, defaultValue = "10") Integer size,
                  @RequestParam(required = false) String name) {

        Page<AclRole> dataPage = aclRoleService.lambdaQuery()
                .like(StringUtils.hasText(name), AclRole::getRole, name)
                .orderByDesc(AclRole::getCreateTime)
                .page(Page.of(page, size));

        return R.page(dataPage.getRecords(), dataPage.getTotal());
    }

    @PreAuthorize("hasAuthority('aclrole:query')")
    @GetMapping("/{id}")
    public R getById(@NotNull(message = "id不能为空") @PathVariable Long id) {
        AclRole aclRole = aclRoleService.getById(id);
        if (aclRole == null) {
            return R.fail("不存在");
        }
        return R.ok(aclRole);
    }

    @PreAuthorize("hasAuthority('aclrole:add')")
    @PostMapping("/save")
    public R add(@Validated AclRole aclRole) {
        if (aclRole.getId() != null) {
            return R.fail("添加不能传ID值");
        }
        aclRole.setCreateTime(LocalDateTime.now());
        aclRole.setUpdateTime(LocalDateTime.now());
        aclRoleService.save(aclRole);

        return R.ok();
    }

    @PreAuthorize("hasAuthority('aclrole:edit')")
    @PutMapping("/edit")
    public R edit(@Validated AclRole aclRole) {
        if (aclRole.getId() == null) {
            return R.fail("请传ID值");
        }
        aclRole.setUpdateTime(LocalDateTime.now());
        aclRoleService.updateById(aclRole);
        return R.ok();
    }


    // 查询该角色现在有的菜单
    @GetMapping("/hasMenus/{roleId}")
    public R hasMenus(@PathVariable Long roleId) {
        List<AclRolePermission> rolePermissionList = aclRolePermissionService.lambdaQuery()
                .eq(AclRolePermission::getRoleId, roleId)
                .list();
        return R.ok(rolePermissionList.stream().map(AclRolePermission::getPermissionId).toList());
    }

    // 分配菜单
    @PostMapping("/setRolePermissions")
    public R setRolePermissions(@Validated SetRolePermissionsVo setRolePermissionsVo) {
        if (setRolePermissionsVo.getMenuIdList().isEmpty()) {
            return R.fail("请勾选菜单");
        }
        aclRoleService.setRolePermissions(setRolePermissionsVo.getRoleId(), setRolePermissionsVo.getMenuIdList());
        return R.ok();
    }


    @PreAuthorize("hasAuthority('aclrole:delete')")
    @DeleteMapping("/{id}")
    public R delete(@NotNull(message = "id不能为空") @PathVariable Long id) {
        AclRole aclRole = aclRoleService.getById(id);
        if (aclRole == null) {
            return R.fail("不存在");
        }
        aclRoleService.removeById(id);
        return R.ok();
    }
}
